
#include <stdio.h>
#include "base/abc/abc.h"
#include "base/main/main.h"


static int Abc_CommandOpt             ( Abc_Frame_t * pAbc, int argc, char ** argv );
static int Abc_CommandStar			  ( Abc_Frame_t * pAbc, int argc, char ** argv );


extern void Demo_Init( Abc_Frame_t * pAbc )
{
    Cmd_CommandAdd( pAbc, "Demo for Optimization",     "opt",   Abc_CommandOpt,       0 );
	Cmd_CommandAdd( pAbc, "Demo for show",     "star",   Abc_CommandStar,       0 );
}

extern void Demo_End( Abc_Frame_t * pAbc )
{
	
}
//对电路进行复制
static Abc_Ntk_t *Ntk_Copy(Abc_Ntk_t *pNtk1)
{
	Abc_Ntk_t *pNtk_Copy = NULL;
	Abc_Obj_t *pObj = NULL;
	int i = 0;
	pNtk_Copy = Abc_NtkDup(pNtk1);
	pNtk1->pCopy = NULL;
	Abc_NtkForEachObj( pNtk1, pObj, i ) {
		pObj->pCopy = NULL;
	}
	return pNtk_Copy;
}

static int Abc_CommandOpt( Abc_Frame_t * pAbc, int argc, char ** argv )
{
    char Buffer[16],c;
    int fVerbose;
	Abc_Ntk_t *pNtk = NULL, *pNtkRes = NULL, *pNtkCopy = NULL;
	
	extern Abc_Ntk_t *Optimaize_resyn2(Abc_Ntk_t *pNtkx);
	
	pNtk = Abc_FrameReadNtk(pAbc);
	if(pNtk == NULL) {
		goto usage;
	}
    // set defaults
    fVerbose =  0;
    Extra_UtilGetoptReset();
    while ( ( c = Extra_UtilGetopt( argc, argv, "vh" ) ) != EOF )
    {
        switch ( c )
        {
        case 'v':
            fVerbose ^= 1;
            break;
        default:
            goto usage;
        }
    }
	pNtkCopy = Ntk_Copy(pNtk);
	pNtkRes = Optimaize_resyn2(pNtkCopy);
	Abc_FrameReplaceCurrentNetwork( pAbc, pNtkRes );
    return 0;

usage:
    Abc_Print( -2, "usage: opt [-vh]\n" );
    return 1;
}


static int Abc_CommandStar( Abc_Frame_t * pAbc, int argc, char ** argv )
{
    char Buffer[16],c;
    int fVerbose;
	Abc_Ntk_t *pNtk = NULL, *pNtkRes = NULL, *pNtkCopy = NULL;
	
	extern int showshow();
	
    // set defaults
    fVerbose =  0;
    Extra_UtilGetoptReset();
    while ( ( c = Extra_UtilGetopt( argc, argv, "vh" ) ) != EOF )
    {
        switch ( c )
        {
        case 'v':
            fVerbose ^= 1;
            break;
        default:
            goto usage;
        }
    }
	
	showshow();
	
    return 0;

usage:
    Abc_Print( -2, "usage: star [-vh]\n" );
    return 1;
}


